<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="问卷题型" prop="infoTypeId">
        <treeselect
          v-model="queryParams.infoTypeId"
          :options="typeOptions"
          :normalizer="normalizer"
          clearable
          style="width: 194px;"
          placeholder="请选择问卷题型"
        />
      </el-form-item>
      <el-form-item label="问卷名称" prop="surveysName">
        <el-input
          v-model="queryParams.surveysName"
          placeholder="请输入问卷名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['surveys:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['surveys:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['surveys:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['surveys:info:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column  label="问卷题型" align="center"  prop="typeName"/>
      <el-table-column label="问卷图标" align="center" prop="picAddr">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.picAddr"  >
            </el-image>
        </template>
      </el-table-column>
      <el-table-column label="问卷编号" align="center" prop="surveysNo" />
      <el-table-column label="问卷名称" align="center" :show-overflow-tooltip="true"  prop="surveysName" />
      <el-table-column label="问题标题" align="center" :show-overflow-tooltip="true"  prop="title" />
      <!-- <el-table-column label="副标题" align="center" prop="fuTitle" /> -->
      <!-- <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="感谢信息" align="center" prop="thanksMsg" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="查看结果" align="center" prop="lookFlag">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.lookFlag==0">显示</el-tag>
          <el-tag type="info" v-else>隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['surveys:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['surveys:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷题型">
          <treeselect
            v-model="form.infoTypeId"
            :options="typeOptions"
            :normalizer="normalizer"
            placeholder="请选择问卷题型"
          />
        </el-form-item>
        <el-form-item label="问卷图标" prop="picAddr">
          <!-- <el-input v-model="form.picAddr" placeholder="请输入问卷图标" /> -->
          <el-upload
  class="avatar-uploader"
  action="/uploadQiniu"
  :show-file-list="false"
  :on-success="handleFileSuccess"
  >
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="问卷编号" prop="surveysNo">
          <el-input v-model="form.surveysNo" placeholder="请输入问卷编号" />
        </el-form-item>
        <el-form-item label="问卷名称" prop="surveysName">
          <el-input v-model="form.surveysName" placeholder="请输入问卷名称" />
        </el-form-item>
        <el-form-item label="问题标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="fuTitle">
          <el-input v-model="form.fuTitle" placeholder="请输入副标题" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="感谢信息" prop="thanksMsg">
          <el-input v-model="form.thanksMsg" placeholder="请输入感谢信息" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="查看结果" prop="lookFlag">
          <el-select v-model="form.lookFlag">
            <el-option value="0" label="允许" key="0">显示</el-option>
            <el-option value="1" label="隐藏" key="1">隐藏</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="stopTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.stopTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import { uploadQiniu } from "@/api/common";
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo} from "@/api/surveys/info";
import { listType, getType } from "@/api/surveys/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Info",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 问卷基础信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 问卷类型树选项
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoTypeId: undefined,
        surveysName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        infoTypeId: [
          { required: true, message: "问卷题型不能为空", trigger: "blur" }
        ],
        surveysNo: [
          { required: true, message: "问卷编号不能为空", trigger: "blur" }
        ],
        surveysName: [
          { required: true, message: "问卷名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getTreeselect();
    
    this.getDicts("surveys_status").then(response => {
      this.statusOptions = response.data;
    });
    
  },
  methods: {
    /** 查询问卷基础信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询问卷类型下拉树结构 */
    getTreeselect() {
      listType().then(response => {
        this.typeOptions = [];
        const data = { infoTypeId: 0, typeName: "顶级节点", children: [] };
        data.children = this.handleTree(
          response.data,
          "infoTypeId",
          "parentId"
        );
        this.typeOptions.push(data);
        this.getList();
      });
    },
    /** 转换问卷类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.infoTypeId,
        label: node.typeName,
        children: node.children
      };
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        infoTypeId: undefined,
        picAddr: undefined,
        surveysNo: undefined,
        surveysName: undefined,
        title: undefined,
        fuTitle: undefined,
        description: undefined,
        thanksMsg: undefined,
        status: 0,
        orderNum: 0,
        totalNum: undefined,
        delFlag: 0,
        lookFlag: "0",
        startTime: undefined,
        stopTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问卷基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问卷基础信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除问卷基础信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有问卷基础信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportInfo(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    handleChange(file, fileList) {
        console.log(file, fileList);
         layer.msg("重新上传");
    },
    handleFileSuccess(file) {
        layer.msg("上传成功");
    }
  }
};
</script>